<?php
declare (strict_types = 1);

namespace app\index\controller;

// use think\Request;
use think\facade\Db;
use think\facade\Cache;
use think\facade\Request;
use think\facade\Config;

class Member{
    //获取会员信息列表（根据openid）
    public function getMemberByOpenId(){
        if(Request::isPost()){
            $openId=input('param.openId',"");
            $data = Db::name('member')->field('member_id,name,CONCAT("***",id_num_last_six) as id_num,mobile')
            ->where(['open_id'=>$openId,'is_delete'=>0])->order('member_id desc')->select();
            return json(['code'=>200,'success'=>true,'message'=>'获取成功','result'=>$data]);
        }
    }

    //新增会员信息
    public function add(){
        if(Request::isPost()){
            try{
                $id_num=input('param.id_num',"");
                $id_num_last_six=substr($id_num, -6);
                $member_id=Db::name('member')->insertGetId([
                    'open_id'=>input('param.openId',""),
                    'name'=>input('param.name',""),
                    'id_type'=>input('param.id_type',0),
                    'encrypt_id_num'=>base64_encode(openssl_encrypt($id_num, 'AES-128-ECB', $id_num_last_six.'.daRong1994.cn', 0)),
                    'id_num_last_six'=>$id_num_last_six,
                    'mobile'=>input('param.mobile',""),
                    'create_time'=>time(),
                ]);
                return json(['code'=>200,'success'=>true,'message'=>'新增成功']);
            }catch (Exception $e) {
                return json(['code'=>200,'success'=>false,'message'=>$e]);
            }
        }
    }

    //删除会员
     public function del(){
        if(Request::isPost()){
            try{
                $member_id=Db::name('member')->where(['member_id'=>input('param.member_id',0),'is_delete'=>0])->update([
                    'is_delete'=>1,
                    'update_time'=>time()
                ]);
                return json(['code'=>200,'success'=>$member_id,'message'=>'删除成功']);
            }catch (Exception $e) {
                return json(['code'=>200,'success'=>false,'message'=>'删除失败:'.$e]);
            }
        }
    }
}